home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / TEST / SQUIGGLE / SQUIGGLE.CPP next >
Encoding:
C/C++ Source or Header  |  1995-08-12  |  5.2 KB  |  235 lines

  1. #include "wfc.h"
  2. #pragma hdrstop
  3.  
  4. void PASCAL fill_data( CSquiggleData *data_p );
  5.  
  6. class CMainWindow : public CFrameWnd
  7. {
  8.    public:
  9.  
  10.       CMainWindow();
  11.  
  12.       CLabeledGrid Grid;
  13.  
  14.    protected:
  15.  
  16.       afx_msg int  OnCreate( LPCREATESTRUCT cs_p );
  17.       afx_msg void OnPaint();
  18.       afx_msg void OnTimer( UINT id );
  19.       DECLARE_MESSAGE_MAP()
  20. };
  21.  
  22. CMainWindow::CMainWindow()
  23. {
  24.    int number_of_rows    = 1;
  25.    int number_of_columns = 1;
  26.  
  27.    Grid.SetSize( number_of_rows, number_of_columns );
  28.  
  29.    int row_index    = 0;
  30.    int column_index = 0;
  31.  
  32.    for ( row_index = 0; row_index < number_of_rows; row_index++ )
  33.    {
  34.       for( column_index = 0; column_index < number_of_columns; column_index++ )
  35.       {
  36.          CSquiggle *squiggle_p = new CSquiggle;
  37.  
  38.          squiggle_p->SetHeight( 480 );
  39.          squiggle_p->SetWidth(  640 );
  40.          squiggle_p->SetLineColor( YELLOW );
  41.          squiggle_p->SetFillColor( BLACK  );
  42.          squiggle_p->SetNumberOfGridLines( 10, 8 );
  43.          squiggle_p->SetLineThickness( 2 );
  44.          squiggle_p->SetGridLineType( PS_SOLID );
  45.  
  46.          CSquiggleData *data_p = new CSquiggleData;
  47.  
  48.          data_p->MinimumValue = 1;
  49.          data_p->MaximumValue = 256;
  50.          data_p->Data.SetSize( 256 );
  51.  
  52.          fill_data( data_p );
  53.  
  54.          squiggle_p->SetSquiggleData( data_p, TRUE );
  55.  
  56.          Grid.SetAt( row_index, column_index, squiggle_p );
  57.       }
  58.    }
  59.  
  60.    TCHAR name[ 10 ];
  61.  
  62.    name[ 0 ] = 'A';
  63.    name[ 1 ] = ' ';
  64.    name[ 2 ] = 'R';
  65.    name[ 3 ] = 'o';
  66.    name[ 4 ] = 'w';
  67.    name[ 5 ] = 0x00;
  68.  
  69.    for ( row_index = 0; row_index < number_of_rows; row_index++ )
  70.    {
  71.       name[ 0 ] = (TCHAR) ( 'A' + row_index );
  72.       Grid.SetRowName( row_index, name );
  73.    }
  74.  
  75.    Grid.SetRowsTitle( "Amplitude" );
  76.  
  77.    for( column_index = 0; column_index < number_of_columns; column_index++ )
  78.    {
  79.       ::sprintf( name, "%02d", column_index );
  80.       Grid.SetColumnName( column_index, name );
  81.    }
  82.  
  83.    Grid.SetColumnsTitle( "Frequency" );
  84.    Grid.SetName( "Battleship!" );
  85.    Grid.SetLabelOptions( LABELED_GRID_COLUMNS_TITLE | LABELED_GRID_ROWS_TITLE );
  86.    Grid.SetVerticalSpacing( 2 );
  87.    Grid.SetHorizontalSpacing( 2 );
  88.    
  89.    CRect rectangle( 90, 60, 0, 0 );
  90.  
  91.    Grid.SetRectangle( rectangle );
  92. }
  93.  
  94. void PASCAL fill_data( CSquiggleData *data_p )
  95. {
  96.    static static_randomizer_initialized = 0;
  97.  
  98.    if ( static_randomizer_initialized == 0 )
  99.    {
  100.       static_randomizer_initialized = 1;
  101.  
  102.       srand( (unsigned) time( NULL ) );
  103.    }
  104.  
  105.    if ( data_p == (CSquiggleData *) NULL )
  106.    {
  107.       return;
  108.    }
  109.  
  110.    int number_of_elements = data_p->Data.GetSize();
  111.    int index              = 0;
  112.  
  113.    WORD random_value = 0;
  114.    WORD valid_range  = data_p->ValidRange();
  115.    WORD last_value   = 0;
  116.    WORD direction    = 0;
  117.    WORD new_value    = 0;
  118.  
  119.    while( index < number_of_elements )
  120.    {
  121.       random_value = (WORD) ( rand() % 67 );
  122.  
  123.       if ( direction == 0 )
  124.       {
  125.          new_value = (WORD) ( last_value + random_value );
  126.          new_value += (WORD) data_p->MinimumValue;
  127.  
  128.          if ( new_value >= data_p->MaximumValue )
  129.          {
  130.             direction = 1;
  131.             new_value = data_p->MaximumValue - random_value;
  132.          }
  133.       }
  134.       else
  135.       {
  136.          if ( last_value < random_value )
  137.          {
  138.             new_value = data_p->MinimumValue + random_value;
  139.             direction = 0;
  140.          }
  141.          else
  142.          {
  143.             new_value = (WORD) ( last_value - random_value );
  144.  
  145.             if ( new_value <= data_p->MinimumValue )
  146.             {
  147.                direction = 0;
  148.                new_value = data_p->MinimumValue;
  149.             }
  150.          }
  151.       }
  152.  
  153.       data_p->Data.SetAt( index, new_value );
  154.  
  155.       last_value = new_value;
  156.  
  157.       index++;
  158.    }
  159. }
  160.  
  161. BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
  162.    ON_WM_CREATE()
  163.    ON_WM_PAINT()
  164.    ON_WM_TIMER()
  165. END_MESSAGE_MAP()
  166.  
  167. int CMainWindow::OnCreate( LPCREATESTRUCT )
  168. {
  169.    SetTimer( 1, 200, NULL );
  170.    return( 0 );
  171. }
  172.  
  173. void CMainWindow::OnPaint()
  174. {
  175.    CPaintDC device_context( this );
  176.  
  177.    Grid.Draw( device_context );
  178. }
  179.  
  180. void CMainWindow::OnTimer( UINT )
  181. {
  182.    CSquiggle *squiggle_p = (CSquiggle *) Grid.GetAt( 0, 0 );
  183.  
  184.    if ( squiggle_p != (CSquiggle *) NULL )
  185.    {
  186.       CSquiggleData *data_p = new CSquiggleData;
  187.  
  188.       if ( data_p != (CSquiggleData *) NULL )
  189.       {
  190.          data_p->MinimumValue = 1;
  191.          data_p->MaximumValue = 256;
  192.          data_p->Data.SetSize( 256 );
  193.  
  194.          fill_data( data_p );
  195.  
  196.          squiggle_p->SetSquiggleData( data_p, TRUE );
  197.  
  198.          CRect rectangle;
  199.  
  200.          squiggle_p->GetRectangle( rectangle );
  201.  
  202.          InvalidateRect( &rectangle, FALSE );
  203.       }
  204.    }
  205. }
  206.  
  207. class CSimpleApplication : public CWinApp
  208. {
  209.    public:
  210.  
  211.       BOOL InitInstance();
  212. };
  213.  
  214. BOOL CSimpleApplication::InitInstance()
  215. {
  216.    CMainWindow *main_window_p = new CMainWindow;
  217.  
  218.    m_pMainWnd = main_window_p;
  219.  
  220.    main_window_p->Create( NULL, "Simple" );
  221.  
  222.    {
  223.       CClientDC device_context( main_window_p );
  224.  
  225.       main_window_p->Grid.PrepareForPainting( device_context );
  226.    }
  227.  
  228.    main_window_p->ShowWindow( m_nCmdShow );
  229.    main_window_p->UpdateWindow();
  230.  
  231.    return( TRUE );
  232. }
  233.  
  234. CSimpleApplication dodah;
  235.